/*
 *报警热区分析
 *
 */

//初始化当前时间
$(function(){
    var center=localStorage.getItem('cityMapCenter').split(',')
    var map = new AMap.Map("container", {
        resizeEnable : true,
        center : center,
        zoom : 8
    });
    window.map=map
	var height = $(document).height() - 135 + 'px';
	$('.alarmHot #container').height(height);
	var todayDate = new Date();
    var todayDateStr=todayDate.getFullYear()+"-"+(todayDate.getMonth()+1)+"-"+todayDate.getDate();      	
    var beforeDate = new Date(todayDate-86400000*1); //前一天
    var yyyy = beforeDate.getFullYear(), mm = (beforeDate.getMonth() + 1).toString(), dd = beforeDate.getDate().toString();
    if (mm.length == 1) {
        mm = '0' + mm;
    }
    if (dd.length == 1) {
        dd = '0' + dd;
    }
    $("#inputStartDate").val(yyyy + '-' + mm + '-' + dd);
    $("#inputEndDate").val(yyyy + '-' + mm + '-' + dd); 
    //加载下拉框
    getSelectData();
})

function getSelectData(){
   $.ajax({    	
    	type : 'get',
		async : true,
		url : appCtx + "report/queryAlarmType.html?time="+ new Date().getTime(),
		cache : true,
		dataType : "JSON",
		success : function(data) {
			var selectStr;
			if(data){
				$.each(data,function(entryIndex,entry) {
					selectStr+="<option value='"+entry.id+"'>"+entry.name+"</option>";
				});
			}
			$('#alarmType').html(selectStr);
			$('#alarmType').select2();
		}
    });
}

var ServerList = [];
var alarmType;
var heatmap;

function queryAnalysisHotInfo() {
	var startTime = $('#inputStartDate').val();
	var endTime = $('#inputEndDate').val();
	alarmType=$('#alarmType').val();
	//查询数据不超过7天
		var arr = startTime.split("-");
	    var starttimes = new Date(arr[0], arr[1], arr[2]).getTime();
	    var arrs = endTime.split("-");
	    var lktimes = new Date(arrs[0], arrs[1], arrs[2]).getTime();
	    if((lktimes-starttimes)/(1000*60*60*24) >=7){
	    	global.ajaxLoading.pop("只能查询7天内的数据", true, 2000);
			return;	
	    } 
	    if((lktimes-starttimes)/(1000*60*60*24) <0){
	    	global.ajaxLoading.pop("开始时间不能大于结束时间", true, 2000);
			return;	
	    } 
	//清空地图数据
	clearHotOverlay(heatmap);  
	global.ajaxLoading.pop('正在拼命加载,请稍候!',true);
	$.ajax({
		type : 'post',
		async : true,
		data : {"startTime":startTime+" "+"00:00:00","endTime":endTime+" "+"23:59:59","alarmType":alarmType},
		url : appCtx + "report/queryAlarmHotAnalysisByTime.html",
		cache : false,
		dataType : "JSON",
		success : function(data) {
		if(data.length==0){
			global.ajaxLoading.pop('无数据存在',false,2000);
			 }else{
				 setHotData(data);
				 global.ajaxLoading.close();
			 }
		 if (!isSupportCanvas()) {
			 global.ajaxLoading.pop("热力图仅对支持canvas的浏览器适用,您所使用的浏览器不能使用热力图功能,请换个浏览器试试~", true, 2000);
			 }
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			global.ajaxLoading.pop('网络连接错误，请检查网络!',false);
			   }
	})	
	global.ajaxLoading.pop('正在拼命加载,请稍候!',false);
}

//判断浏览区是否支持canvas
function isSupportCanvas() {
	var elem = document.createElement('canvas');
	return !!(elem.getContext && elem.getContext('2d'));
}

function setHotData(data){
	map.plugin([ "AMap.Heatmap" ], function(){
		heatmap =new AMap.Heatmap(map,{"radius":25},{opacity : [ 0, 0.8 ]}); 
		heatmap.setDataSet({
	            data: data,
	            max: 100
	      });
	});
	
}
function clearHotOverlay(heatmap){
	if(undefined==heatmap){
		return;
	}
	map.remove(heatmap);
}


